package com.test.security.springsecuritydemo10.filter;

import java.io.IOException;
import javax.servlet.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.web.csrf.CsrfToken;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： liuziyang
 * @date： 2024/10/8-16:39
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
public class CsrfTokenLogger implements Filter {
  private static final Logger LOGGER = LoggerFactory.getLogger(CsrfTokenLogger.class);

  /**
   * @param request The request to process
   * @param response The response associated with the request
   * @param chain Provides access to the next filter in the chain for this filter to pass the
   *     request and response to for further processing
   * @throws IOException
   * @throws ServletException
   */
  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      throws IOException, ServletException {
    final Object csrf = request.getAttribute("_csrf");
    CsrfToken token = (CsrfToken) csrf;
    LOGGER.info("CSRF token {}", token.getToken());
    chain.doFilter(request, response);
  }
}
